﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using DiffuseEnjoyIdle.Core.Helper;
using DiffuseEnjoyIdle.EntityFrameworkCore;
using DiffuseEnjoyIdle.Extension.Extensions;
using DiffuseEnjoyIdle.Extension.Extensions.Mapster;
using DiffuseEnjoyIdle.Model.Config;
using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System;
using System.IO;
using System.Reflection;
using Xunit.DependencyInjection;
using Xunit.DependencyInjection.Logging;

namespace DiffuseEnjoyIdle.Test
{
    public class Startup
    {
        public void ConfigureHost(IHostBuilder hostBuilder)
        {
            hostBuilder.ConfigureHostConfiguration(builder =>
            {
                builder.AddJsonFile("appsettings.json", true);
                //.AddApolloExtension();
            }).UseServiceProviderFactory(new AutofacServiceProviderFactory(a =>
            {
                a.RegisterModule(new AutofacModuleRegister());
            }));
        }

        public void Configure(ILoggerFactory loggerFactory, IServiceProvider serviceProvider, ITestOutputHelperAccessor outputHelperAccessor)
        {
            loggerFactory.AddProvider(new XunitTestOutputLoggerProvider(outputHelperAccessor));
            serviceProvider.UseStateAutoMapper();
        }

        public void ConfigureServices(IServiceCollection services, HostBuilderContext hostBuilderContext)
        {
            var configuration = hostBuilderContext.Configuration;
            services.Configure<RabbitMQConfig>(configuration.GetSection("RabbitMQConfig"));

            services.AddDbContext<DBContext>(opt =>
            {
                var s = configuration.GetConnectionString("mysql");
                opt.UseMySql(configuration.GetConnectionString("mysql"), ServerVersion.Parse("8.0.21-mysql"));
            });
            services.AddMQHandler();

            //// services.AddSingleton(new InitMapsterAdaptConifg().InitMapperConfig());
            services.AddSingleton(p =>
            {
                var connStr = configuration.GetConnectionString("redis");
                return ConnectionMultiplexer.Connect(connStr);
            });
        }
    }
}